/*
 * Copyright (c) 2022 Proton Technologies AG
 * This file is part of Proton Technologies AG and Proton Mail.
 *
 * Proton Mail is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Proton Mail is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Proton Mail. If not, see <https://www.gnu.org/licenses/>.
 */

package ch.protonmail.android.mailcommon.domain.usecase

import ch.protonmail.android.mailcommon.domain.MailFeatureDefaults
import ch.protonmail.android.mailcommon.domain.MailFeatureId
import ch.protonmail.android.mailcommon.domain.sample.UserIdSample
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import me.proton.core.featureflag.domain.FeatureFlagManager
import me.proton.core.featureflag.domain.entity.FeatureFlag
import me.proton.core.featureflag.domain.entity.Scope
import org.junit.Test
import kotlin.test.assertEquals

class ObserveMailFeatureTest {

    private val featureFlagManagerMock: FeatureFlagManager = mockk()

    @Test
    fun `should return the requested feature flag if available`() = runTest {
        // Given
        val observeMailFeature = observeMailFeatureWithDefaults(emptyMap())
        val requestedFeature = MailFeatureId.ConversationMode
        val expectedFeatureFlag = FeatureFlag(
            userId = UserIdSample.Primary,
            featureId = requestedFeature.id,
            value = true,
            scope = Scope.User,
            defaultValue = true
        )
        every {
            featureFlagManagerMock.observe(UserIdSample.Primary, requestedFeature.id)
        } returns flowOf(expectedFeatureFlag)

        // When
        val actualFeatureFlag = observeMailFeature(UserIdSample.Primary, requestedFeature).first()

        // Then
        assertEquals(expectedFeatureFlag, actualFeatureFlag)
    }

    @Test
    fun `should return the default feature flag if failed to retrieve`() = runTest {
        // Given
        val requestedFeature = MailFeatureId.ConversationMode
        val defaultsMap = mapOf(requestedFeature to true)
        val observeMailFeature = observeMailFeatureWithDefaults(defaultsMap)
        val expectedFeatureFlag = FeatureFlag(
            userId = UserIdSample.Primary,
            featureId = requestedFeature.id,
            value = true,
            scope = Scope.Unknown,
            defaultValue = true
        )
        every {
            featureFlagManagerMock.observe(UserIdSample.Primary, requestedFeature.id)
        } returns flowOf(null)

        // When
        val actualFeatureFlag = observeMailFeature(UserIdSample.Primary, requestedFeature).first()

        // Then
        assertEquals(expectedFeatureFlag, actualFeatureFlag)
    }

    private fun observeMailFeatureWithDefaults(defaultsMap: Map<MailFeatureId, Boolean>) =
        ObserveMailFeature(featureFlagManagerMock, MailFeatureDefaults(defaultsMap))
}
